import axios from 'axios'
import { method } from 'lodash'
import { showToast } from 'vant'

const service = axios.create({
  baseURL: '/qdxt',
  timeout: 30000
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    const tempInfo=localStorage.getItem('userInfo');
    const userInfo =tempInfo=='undefined'?{}: JSON.parse(localStorage.getItem('userInfo'))
    
    if (userInfo&&userInfo.openid) {
      config.headers['X-OpenId'] = userInfo.openid
    }
    if (method == 'POST') {
        config.headers['content-type'] = 'application/json;charset=UTF-8'
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    showToast(error.message)
    return Promise.reject(error)
  }
)

export default {
  // 绑定接口
  bindPhone: (data) => service.post('/sendnc/dobusiness', data),
  
  // 获取用户信息
  getUserByOpenId: (openId) => service.get(`/user/openId?openId=${openId}`),
  
  //根据OPENID查询用户信息
  getUSERByOpenId:(data) => service.post('/sendnc/dobusiness',data),
  
  // 物料列表
  getProductList: (data) => service.post('/sendnc/dobusiness', data),
  
  // 提交订单
  submitOrder: (data) => service.post('/sendnc/dobusiness', data),
  
  // 查询订单
  queryOrders: (data) => service.post('/sendnc/dobusiness', data),
  
  // 删除订单
  deleteOrder: (data) => service.post('/sendnc/dobusiness',data),
  
  // 上传签名
  uploadSign:(data)=>service.post('/sendnc/dobusiness',data)
}